package com.kcsj.service.user;

import com.kcsj.dao.User.CustomerDao;
import com.kcsj.dao.User.RoleDao;
import com.kcsj.dao.User.UserDao;
import com.kcsj.domain.user.Customer;
import com.kcsj.domain.user.Role;
import com.kcsj.domain.user.User;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;
import com.kcsj.util.SendMailUitls;
import com.kcsj.util.UUIDUtils;

import javax.mail.MessagingException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @author 胡庆阳/傅立贤
 * 29th Nov. 2020
 */
public class CustomerService {
    private CustomerDao customerDao = CustomerDao.getInstance();
    private static CustomerService customerService = new CustomerService();

    public CustomerService() {
    }
    public static CustomerService getInstance(){
        return CustomerService.customerService;
    }
    public Collection<User> findAll(List<Condition> conditionList) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<User> users = customerDao.findAll(conn, conditionList);
        JdbcHelper.close(conn);
        return users;
    }

    public User find(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        User user = customerDao.find(id, conn);
        JdbcHelper.close(conn);
        return user;
    }

    public Customer findInfo(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Customer info = customerDao.findInfo(id, conn);
        JdbcHelper.close(conn);
        return info;
    }



    public void addUserOfCustomer(User user) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        user.setType("客户");
        user.setActivationStatus(true);
        Role role = RoleDao.getInstance().find(7, conn);
        user.setRole(role);
        customerDao.add(user,conn);
        JdbcHelper.close(conn);
    }


    //注册
    public void register(User user) throws SQLException,
            MessagingException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        try {
            //获得随机生成的code
            String code = UUIDUtils.getUUID();
            //user设定用户类型
            user.setType("客户");
            Role role = RoleDao.getInstance().find(7,conn);
            user.setRole(role);
            //初始账号未激活
            user.setActivationStatus(false);
            //为user设置code
            user.setCode(code);
            Customer addCustomer = customerDao.add(user, conn);
            //发送邮件
            SendMailUitls.sendMail(addCustomer.getEmail(),user.getCode());
            conn.commit();
        }catch (SQLException e){
            e.printStackTrace();
            conn.rollback();
        }finally {
            conn.setAutoCommit(false);
            JdbcHelper.close(conn);
        }
    }

    public void updateInfo(User user) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        try{
            UserDao.getInstance().update(user,conn);
            Customer customer = (Customer) user.getActor();;
            customerDao.update(customer,conn);
            conn.commit();
        }catch (SQLException e){
            e.printStackTrace();
            conn.rollback();
        }finally {
            conn.setAutoCommit(false);
            JdbcHelper.close(conn);
        }
    }


    public boolean delete(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean delete = false;
        try{
            delete = customerDao.delete(id,conn);
            delete = UserDao.getInstance().delete(id, conn);
            conn.commit();
        }catch (SQLException e){
            e.printStackTrace();
            conn.rollback();
        }finally {
            conn.setAutoCommit(false);
            JdbcHelper.close(conn);
        }
        return delete;
    }

}
